package com.ultrapower.charts.delaunay
{
	import de.polygonal.ds.Collection;
	import de.polygonal.ds.Iterator;

	public class ArraySet implements Collection 
	{
		private var items:Array = [];
		
		public function ArraySet(col:Collection){
			if(col!=null){
				var iterator:Iterator = col.iterator;
				while(iterator.hasNext()){
					var item:Object = iterator.next();
					if (!this.contains(item)){
						items.push(item);
					} 
				}
			}			
		}
				
		
		public function getItemAt(index:int):Object{
			var result:Object;
			if(index<items.length){
				result = items[index];
			}
			return result;
		}
		
		public function addItem(item:Object):Boolean{
			if(contains(item)){
				return false;
			}else{
				items.push(item);
				return true;
			}
		}
		
		public function contains(item:*):Boolean{
			var result:Boolean = false;
			for each(var obj:Object in items){
				if(obj == item){
					result = true;
					break;
				}
			}
			return result;
		}
		
		public function get iterator():Iterator{			
			var iter:ArrayIterator = new ArrayIterator(items);
			return iter;
		}
		public function get data():Array{
			return items;
		}
		
		public function clear():void{
			items.splice(0,items.length-1);
		}
		
		public function isEmpty():Boolean{
			return items.length == 0;
		}
		
		public function get size():int{
			return items.length;
		}
		
		public function toArray():Array{
			return items;
		}
		
		public function removeItem(obj:Object):Boolean{
			var result:Boolean = false;
			if(contains(obj)){
				for(var i:int=0;i<items.length;i++){
					if(items[i] == obj){
						items.splice(i,1);
						result = true;
						break;
					}
				}
			}
			return result;
		}
		
		public function removeItemAt(index:int):Boolean{
			var result:Boolean = false;
			if(index<items.length){
				items.splice(index,1);
				result = true;
			}
			return result;
		}
		
		public function containsAny(collect:Collection):Boolean{
			var result:Boolean = false;
			var iterator:Iterator = collect.iterator;			
			while(iterator.hasNext()){
				var item:Object = iterator.next();
				if(this.contains(item)){
					result = true;
					break;
				}
			}
			return result;
		}
		
		public function toString():String{
			var arrset:String = "ArraySet: "
			for each(var item:Object in items){
				arrset += item.toString()+",";
			}
			return arrset;
		}
		
	}//end of ArraySet
}

import de.polygonal.ds.Iterator;

class ArrayIterator implements Iterator
{	
	private var _a:Array;
	private var _cursor:int;
	private var _size:int;
	
	public function ArrayIterator(arr:Array)
	{		
		_a = arr;
		_size = _a.length;
	}
	
	public function next():*
	{		
		var obj:Object = _a[_cursor];
		_cursor++;
		return obj;		
	}
	
	public function hasNext():Boolean
	{
		return _cursor < _size;
	}
	
	public function start():void
	{
		_cursor = 0;
	}

	public function get data():*
	{
		return _a[_cursor];
	}
	
	public function set data(obj:*):void
	{				
		_a[_cursor] = obj;
	}	
}